/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VistaConsultarPoderes.java
 *
 * Created on 09/12/2011, 11:20:49 PM
 */
package appbastanteo1.view;

import appbastanteo1.controller.AdmBastanteos;
import appbastanteo1.model.Bastanteo;
import appbastanteo1.model.Poder;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author JuanJose.Fuchs
 */
public class VistaConsultarPoderes extends javax.swing.JInternalFrame {

    private DateFormat formatter;
    private AdmBastanteos admBastanteos;
    private DefaultTableModel mdlBastanteos;

    /** Creates new form VistaConsultarPoderes */
    public VistaConsultarPoderes(AdmBastanteos admBastanteos) {
        initComponents();

        formatter = new SimpleDateFormat("dd/MM/yyyy");

        this.admBastanteos = admBastanteos;
        mdlBastanteos = (DefaultTableModel) tblBastanteos.getModel();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        cboTipo = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        txtRUCCliente = new javax.swing.JTextField();
        cboTipDocRepr = new javax.swing.JComboBox();
        txtNroDocRepr = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        btnBuscar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblBastanteos = new javax.swing.JTable();

        setClosable(true);
        setTitle("Consultar Poderes");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Filtros"));

        jLabel3.setText("Tipo de intervención");

        cboTipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<Cualquiera>", "A sola firma", "En conjunto con" }));

        jLabel2.setText("Representante");

        cboTipDocRepr.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "DNI", "Pasaporte", "RUC" }));

        jLabel1.setText("RUC Cliente");

        btnBuscar.setText("Buscar");
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGap(39, 39, 39)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(cboTipo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txtRUCCliente)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(cboTipDocRepr, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtNroDocRepr, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGap(18, 18, 18)
                .addComponent(btnBuscar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtRUCCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnBuscar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cboTipDocRepr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(txtNroDocRepr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cboTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tblBastanteos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Tipo de intervención", "Fecha de vencimiento", "Importe", "Poderes"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Double.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblBastanteos);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 617, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
        String rucCliente = txtRUCCliente.getText();
        String tipoDocRep = cboTipDocRepr.getSelectedItem().toString();
        String nroDocRep = txtNroDocRepr.getText();
        String tipoIntervencion = cboTipo.getSelectedItem().toString();
        if (tipoIntervencion.equals("<Cualquiera>")) {
            tipoIntervencion = "";
        }

        ArrayList<Bastanteo> bastanteos = admBastanteos.encontrarBastanteos(
                rucCliente, tipoDocRep, nroDocRep, tipoIntervencion);
        mostrarBastanteos(bastanteos);

    }//GEN-LAST:event_btnBuscarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscar;
    private javax.swing.JComboBox cboTipDocRepr;
    private javax.swing.JComboBox cboTipo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblBastanteos;
    private javax.swing.JTextField txtNroDocRepr;
    private javax.swing.JTextField txtRUCCliente;
    // End of variables declaration//GEN-END:variables

    private void mostrarBastanteos(ArrayList<Bastanteo> bastanteos) {
        mdlBastanteos.setRowCount(bastanteos.size());
        int fila = 0;
        for (Bastanteo bastanteo : bastanteos) {
            mdlBastanteos.setValueAt(bastanteo.getTipoIntervencion(), fila, 0);
            mdlBastanteos.setValueAt(formatter.format(bastanteo.getFechaVencimiento()), fila, 1);
            mdlBastanteos.setValueAt(Double.valueOf(bastanteo.getImporte()), fila, 2);
            mdlBastanteos.setValueAt(concatenarPoderes(bastanteo.getPoderes()), fila, 3);
            fila++;
        }
        if (bastanteos.size() == 0) {
            JOptionPane.showMessageDialog(this, "No se encontraron bastanteos para " +
                    "los criterios ingresados");
        }
    }

    private String concatenarPoderes(ArrayList<Poder> poderes) {
        String cadena = "";
        for (Poder poder : poderes) {
            if (!cadena.isEmpty()) {
                cadena += ", ";
            }
            cadena += poder.getCodigoCorto();
        }
        return cadena;
    }
}
